package controls

import (
	"github.com/go-redis/redis"
	"math/rand"
	"sofuny/config"
	"sofuny/models"
	"time"
)

// app

var app = config.App()

// 数据库
var db = models.ConnectionDB()

// redis
var redisClient = redis.NewClient(&redis.Options{
	Addr:     "127.0.0.1:6379",
	Password: "", // no password set
	DB:       0,  // use default DB
})

// 返回句子数据
type SentenceResponse struct {
	ID         int    `json:"id"`
	CreateAt   string `json:"create_at"`
	Source     string `json:"source"`
	Content    string `json:"content"`
	CopyCounts int    `json:"copyCounts"`
}

//　获取发送数据
func RandomSentenceForEmail() models.Sentence {
	var sentences []models.Sentence
	// 获取数据
	if err := db.Where("status=?", true).Find(&sentences).Error; err != nil {
		return models.Sentence{}
	}
	// 随机一个句子
	rand.Seed(time.Now().UnixNano())
	sentence := sentences[rand.Intn(len(sentences))]
	return sentence
}

// 订阅邮箱
func AllSubscribes() []models.Subscribe {
	var subscribes []models.Subscribe
	if err := db.Select("email, type").Where("status=?", true).Find(&subscribes).Error; err != nil {
		return []models.Subscribe{}
	}
	return subscribes
}
